perf(SelectPanel): Optimize performance by improving the lookup algorithms#7497
perf(SelectPanel): Optimize performance by improving the lookup algorithms#7497hectahertz merged 4 commits intomainfrom
Conversation
🦋 Changeset detectedLatest commit: 6b558de The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
👋 Hi, this pull request contains changes to the source code that github/github-ui depends on. If you are GitHub staff, test these changes with github/github-ui using the integration workflow. Or, apply the |
There was a problem hiding this comment.
Pull request overview
This PR focuses on improving the performance of SelectPanel by replacing repeated linear scans over the items/selected arrays with precomputed Set-based lookups.
Changes:
- Added
itemsInViewSetto speed up “Select all” handling, especially when filtering, by doing O(1) membership checks instead ofitems.some(...). - Added
selectedItemsSetto makeisItemCurrentlySelectedO(1) for multi-select panels. - Updated
itemsToRendersorting to precompute aselectedOnSortSetand use it for determining whether items should be ordered first whenshouldOrderSelectedFirstis enabled.
264e97b to
6b558de
Compare
|
👋 Hi from github/github-ui! Your integration PR is ready: https://github.com/github/github-ui/pull/14286 |
Closes https://github.com/github/primer/issues/5788
This PR improves the performance of SelectPanel by optimizing how selected items are looked up during rendering and sorting.
The previous implementation used O(n) or O(n*m) array lookups when:
Changelog
First, the sorting algorithm in itemsToRender was slow. For each comparison during sorting, it was calling selectedOnSort.some with Object.entries and every, checking all properties of all selected items. I replaced this with a pre-computed Set of selected item IDs that allows instant lookups.
Second, the isItemCurrentlySelected function was iterating through all selected items every time it was called. Since it gets called for every item during the map operation, this was doing a lot of redundant work. I added a memoized selectedItemsSet that gets computed once when the selected prop changes, making each lookup instant.
Third, the handleSelectAllChange function had the same problem. It was using items.some inside a filter, iterating through all visible items for each selected item. I added a memoized itemsInViewSet to make those lookups instant as well.
Rollout strategy
Testing & Reviewing
Merge checklist